---
sidebar_label: Filter using geospatial data
sidebar_position: 4
description: Filter query results and search queries on MS SQL Server in Hasura
keywords:
  - hasura
  - docs
  - ms sql server
  - query
  - filter
  - search
toc_max_heading_level: 3
---

import GraphiQLIDE from '@site/src/components/GraphiQLIDE';

# MS SQL Server: Filter Using Geospatial Data

## Introduction

The `_st_contains`, `_st_crosses`, `_st_equals`, `_st_intersects`, `_st_overlaps`, `_st_touches`, `_st_within` operators
are used to filter based on `geometry` like columns.

:::info <code>\_st_intersects</code> usage

`_st_intersects` can be used on `geography` columns also.

:::

Use JSON representation (see [GeoJSON](https://tools.ietf.org/html/rfc7946)) of `geometry` and `geography` values in
`variables` as shown in the following examples:

## \_st_contains

Fetch a list of geometry values that are contained within the given `polygon` value:

<GraphiQLIDE
  query={`query geom_table($polygon: geometry){
  geom_table(
    where: {geom_col: {_st_contains: $polygon}}
  ){
    id
    geom_col
  }
}`}
  variables={`{
  "polygon": {
    "type": "Polygon",
    "coordinates": [
      [
        [ 1, 1 ],
        [ 1, 3 ],
        [ 3, 3 ],
        [ 3, 1 ],
        [ 1, 1 ]
      ]
    ]
  }
}`}
  response={`{
  "data": {
    "geom_table": [
      {
        "id": 7,
        "geom_col": {
          "type": "Point",
          "coordinates": [
            2,
            2
          ]
        }
      }
    ]
  }
}`}
/>

## \_st_crosses

Fetch a list of geometry values which cross the given `line` value:

<GraphiQLIDE
  query={`query geom_table($line: geometry){
  geom_table(
    where: {geom_col: {_st_crosses: $line}}
  ){
    id
    geom_col
  }
}`}
  variables={`{
  "line": {
    "type": "LineString",
    "coordinates": [
      [ 0, 0 ],
      [ 2, 2 ]
    ]
  }
}`}
  response={`{
  "data": {
    "geom_table": [
      {
        "id": 2,
        "geom_col": {
          "type": "LineString",
          "coordinates": [
            [ 1, 0 ],
            [ 1, 3 ]
          ]
        }
      }
    ]
  }
}`}
/>

## \_st_equals

Fetch a list of geometry values which are equal to the given `polygon` value:

<GraphiQLIDE
  query={`query geom_table($polygon: geometry){
  geom_table(
    where: {geom_col: {_st_equals: $polygon}}
  ){
    id
    geom_col
  }
}`}
  variables={`{
  "polygon": {
    "type": "Polygon",
    "coordinates": [
      [
        [ 0, 0 ],
        [ 0, 2 ],
        [ 2, 2 ],
        [ 2, 0 ],
        [ 0, 0 ]
      ]
    ]
  }
}`}
  response={`{
  "data": {
    "geom_table": [
      {
        "id": 3,
        "geom_col": {
          "type": "Polygon",
          "coordinates": [
            [
              [ 0, 0 ],
              [ 0, 2 ],
              [ 2, 2 ],
              [ 2, 0 ],
              [ 0, 0 ]
            ]
          ]
        }
      }
    ]
  }
}`}
/>

## \_st_intersects

Fetch a list of geometry values which intersect the given `polygon` value:

<GraphiQLIDE
  query={`query geom_table($polygon: geometry){
  geom_table(
    where: {geom_col: {_st_intersects: $polygon}}
  ){
    id
    geom_col
  }
}`}
  variables={`{
  "polygon": {
    "type": "Polygon",
    "coordinates": [
      [
        [ 1, 1 ],
        [ 1, 3 ],
        [ 3, 3 ],
        [ 3, 1 ],
        [ 1, 1 ]
      ]
    ]
  }
}`}
  response={`{
  "data": {
    "geom_table": [
      {
        "id": 4,
        "geom_col": {
          "type": "Point",
          "coordinates": [
            2,
            2
          ]
        }
      }
    ]
  }
}`}
/>

## \_st_overlaps

Fetch a list of geometry values which overlap with the given `polygon` value:

<GraphiQLIDE
  query={`query geom_table($polygon: geometry){
  geom_table(
    where: {geom_col: {_st_overlaps: $polygon}}
  ){
    id
    geom_col
  }
}`}
  variables={`{
  "polygon": {
    "type": "Polygon",
    "coordinates": [
      [
        [ 1, 1 ],
        [ 1, 3 ],
        [ 3, 3 ],
        [ 3, 1 ],
        [ 1, 1 ]
      ]
    ]
  }
}`}
  response={`{
  "data": {
    "geom_table": [
      {
        "id": 5,
        "geom_col": {
          "type": "Polygon",
          "coordinates": [
            [
              [ 2, 2 ],
              [ 2, 4 ],
              [ 4, 4 ],
              [ 4, 2 ],
              [ 2, 2 ]
            ]
          ]
        }
      }
    ]
  }
}`}
/>

## \_st_touches

Fetch a list of geometry values which touch the given `polygon` value:

<GraphiQLIDE
  query={`query geom_table($polygon: geometry){
  geom_table(
    where: {geom_col: {_st_touches: $polygon}}
  ){
    id
    geom_col
  }
}`}
  variables={`{
  "polygon": {
    "type": "Polygon",
    "coordinates": [
      [
        [ 0, 0 ],
        [ 0, 2 ],
        [ 2, 2 ],
        [ 2, 0 ],
        [ 0, 0 ]
      ]
    ]
  }
}`}
  response={`{
  "data": {
    "geom_table": [
      {
        "id": 6,
        "geom_col": {
          "type": "LineString",
          "coordinates": [
            [ 0, 2 ],
            [ 2, 2 ]
          ]
        }
      }
    ]
  }
}`}
/>

## \_st_within

Fetch a list of geometry values which are within the given `polygon` value:

<GraphiQLIDE
  query={`query geom_table($polygon: geometry){
  geom_table(
    where: {geom_col: {_st_within: $polygon}}
  ){
    id
    geom_col
  }
}`}
  variables={`{
  "polygon": {
    "type": "Polygon",
    "coordinates": [
      [
        [ 0, 0 ],
        [ 0, 2 ],
        [ 2, 2 ],
        [ 2, 0 ],
        [ 0, 0 ]
      ]
    ]
  }
}`}
  response={`{
  "data": {
    "geom_table": [
      {
        "id": 1,
        "geom_col": {
          "type": "Point",
          "coordinates": [
            1,
            2
          ]
        }
      }
    ]
  }
}
`}
/>
